﻿using System.Linq;
using Microsoft.EntityFrameworkCore;
using ZSZ.DTO;
using ZSZ.Application.IService;
using ZSZ.Core.Entities;
using ZSZ.EntityFramework;

namespace ZSZ.Application.Service
{
    public class RegionService : Repository<RegionEntity>, IRegionService
    {
        private RegionDTO ToDTO(RegionEntity region)
        {
            var dto = new RegionDTO
            {
                CityId = region.CityId,
                CityName = region.City.Name,
                CreateDateTime = region.CreateDateTime,
                Id = region.Id,
                Name = region.Name
            };
            return dto;
        }
        public RegionDTO[] GetAll(long cityId)
        {
            return GetAllDataEntity().Include(r=>r.City)
                .Where(r => r.CityId == cityId).ToList()
                .Select(r=>ToDTO(r)).ToArray();
        }

        public RegionDTO GetById(long id)
        {
            var region = GetAllDataEntity().Include(r => r.City)
                .SingleOrDefault(r => r.Id == id);
            return region == null ? null : ToDTO(region);
            
        }

        public RegionService(ZSZDbContext context) : base(context)
        {
        }
    }
}
